<?php


namespace app\admin\controller\manage;


use app\admin\model\Reply;
use app\admin\model\ReplyInfo;
use app\common\Common;
use think\facade\Validate;
use think\Request;

class Info extends Common
{
    public function addCate(Request $request){
        $name = $request->param("name","");
        $user = $request->_user;
        $res = Reply::add($name,$user->duty_id,$user->staff_no);
        if ($res){
            return failedJson($res);
        }
        return successJson("success","成功");
    }

    public function upCate(Request $request){
        $id = $request->param("id",0);
        $name = $request->param("name","");
        $res = Reply::up($id,$name);
        if ($res){
            return failedJson($res);
        }
        return successJson("success","成功");
    }

    public function delCate(Request $request){
        $id = $request->param("id",[]);
        $res = Reply::del($id);
        if ($res){
            return failedJson($res);
        }
        return successJson("success","成功");
    }

    public function liCate(Request $request){
        $page = $request->param("page",0);
        $limit = $request->param("limit",10);
        $res = Reply::li($page,$limit);
        return successJson("success",$res);
    }

    public function addInfo(Request $request){
        $data = $request->param();
        $validate = Validate::make([
            "reply_id|分类id" => "require",
            "name|标题" => "require",
            "info|回复内容" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $user = $request->_user;
        $res = ReplyInfo::add($data["reply_id"],$data["name"],$data["info"],$user->staff_no,$user->duty_id);
        if (!$res){
            return failedJson("添加失败");
        }
        return successJson("success","成功");
    }

    public function upInfo(Request $request){
        $data = $request->param();
        $validate = Validate::make([
            "id|回复id"=>"require",
            "reply_id|分类id" => "require",
            "name|标题" => "require",
            "info|回复内容" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $res = ReplyInfo::up($data["id"],$data["reply_id"],$data["name"],$data["info"]);
        if ($res){
            return failedJson($res);
        }
        return successJson("success","成功");
    }

    public function delInfo(Request $request){
        $id = $request->param("id",[]);
        $res = ReplyInfo::del($id);
        if ($res){
            return failedJson($res);
        }
        return successJson("success","成功");
    }

    public function liInfo(Request $request){
        $search = $request->param("search","");
        $replyId = $request->param("reply_id","");
        $createNo = $request->param("staff_no","");
        $page = $request->param("page",0);
        $limit = $request->param("limit",10);
        $list = ReplyInfo::li($search,$createNo, $replyId, $page, $limit);
        return successJson("success",$list);
    }

    public function liMyInfo(Request $request){
        $info = $request->param("info","");
        $createNo = $request->param("staff_no","");
        $replyId = $request->param("reply_id","");
        $page = $request->param("page",0);
        $limit = $request->param("limit",10);
        $list = ReplyInfo::liMy($info,$createNo,$replyId,$page,$limit);
        return successJson("success",$list);
    }

    public function detInfo(Request $request){
        $id = $request->param("id",0);
        $resI = ReplyInfo::where("id",$id)->find();
        if(!$resI){
            return failedJson("数据不存在~");
        }
        $res = ReplyInfo::det($id);
        return successJson("success",$res);
    }
}